package com.example.budget3.modify.dialogs;


import java.util.Calendar;
import java.util.Date;

import com.example.budget3.R;
import com.example.budget3.modify.ChangeTransaction;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TimePicker;

public class TimeAndDateDialogFragment extends DialogFragment {
	
	private DatePicker datePicker;
	private TimePicker timePicker;
	private Button timeButton;
	private Button dateButton;
	private Calendar calendar;
	private int year;
	private int month;
	private int day;
	private int hour;
	private int minute;
	private boolean selected;

	@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
		

		if(savedInstanceState!=null){
				year = savedInstanceState.getInt("year");
				month = savedInstanceState.getInt("month");
				day = savedInstanceState.getInt("day");
				hour = savedInstanceState.getInt("hour");
				minute = savedInstanceState.getInt("minute");
				selected = savedInstanceState.getBoolean("selected"); 
				calendar = Calendar.getInstance();
				calendar.set(year, month, day, hour, minute);
		}else{  
			calendar = Calendar.getInstance();
			calendar.setTimeInMillis(getArguments().getLong("date"));
			year = calendar.get(Calendar.YEAR);
			month = calendar.get(Calendar.MONTH);
	        day = calendar.get(Calendar.DAY_OF_MONTH); 
			hour = calendar.get(Calendar.HOUR_OF_DAY);
	        minute = calendar.get(Calendar.MINUTE);
	        selected = true;
		}
		
		LayoutInflater layoutInflater = getActivity().getLayoutInflater();
		View inflate = layoutInflater.inflate(R.layout.time_and_date_dialog, null);

		datePicker = new DatePicker(getActivity());
    	datePicker.setCalendarViewShown(false);
    	datePicker.setMaxDate(new Date().getTime());
    	datePicker.init(year, month, day, null);
    	   
    	timePicker = new TimePicker(getActivity());
    	timePicker.setIs24HourView(false);
    	timePicker.setCurrentHour(hour);
   		timePicker.setCurrentMinute(minute);
   		
   		timeButton = (Button) inflate.findViewById(R.id.timeButton);
   		dateButton = (Button) inflate.findViewById(R.id.dateButton);
   		
   		if(selected){
	   		datePicker.setVisibility(View.GONE);
			timePicker.setVisibility(View.VISIBLE);
			timeButton.setTextColor(getResources().getColor(R.color.dark_text));
			dateButton.setTextColor(getResources().getColor(R.color.large_button_text));
   		}else{
	   		
			timePicker.setVisibility(View.GONE);
			datePicker.setVisibility(View.VISIBLE);
			timeButton.setTextColor(getResources().getColor(R.color.large_button_text));
			dateButton.setTextColor(getResources().getColor(R.color.dark_text));
   		}
		
   		
   		
   		timeButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				timeButton.setTextColor(getResources().getColor(R.color.dark_text));
				dateButton.setTextColor(getResources().getColor(R.color.large_button_text));
				datePicker.setVisibility(View.GONE);
				timePicker.setVisibility(View.VISIBLE);
				selected = true;
			}
		});
		
   		
   		dateButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dateButton.setTextColor(getResources().getColor(R.color.dark_text));
				timeButton.setTextColor(getResources().getColor(R.color.large_button_text));
				timePicker.setVisibility(View.GONE);
				datePicker.setVisibility(View.VISIBLE);
				selected = false;
			}
		});


   		LinearLayout linear =  (LinearLayout) inflate.findViewById(R.id.container);
   		linear.setGravity(Gravity.CENTER);
 	    linear.addView(timePicker);
 	    linear.addView(datePicker);
 	    linear.setOrientation(LinearLayout.VERTICAL);
		
		AlertDialog.Builder adb = new AlertDialog.Builder( getActivity());
	    adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            	day = datePicker.getDayOfMonth();
            	month = datePicker.getMonth();
            	year = datePicker.getYear();
            	hour = timePicker.getCurrentHour();
            	minute = timePicker.getCurrentMinute();
            	((ChangeTransaction) getActivity()).setTimeAndDate(minute,hour,day,month,year);
            }
        }).setNeutralButton("Reset",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {}
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {}
        });
		
	    
	    AlertDialog dialogBox = adb.setView(inflate).create();
	    dialogBox.show();
	    
	    Button resetButton = dialogBox.getButton(DialogInterface.BUTTON_NEUTRAL);
	    resetButton.setOnClickListener(new View.OnClickListener() {

	        @Override
	        public void onClick(View onClick) {                 
	        	final Calendar c = Calendar.getInstance();
        		year = c.get(Calendar.YEAR);
        		month = c.get(Calendar.MONTH);
                day = c.get(Calendar.DAY_OF_MONTH); 
        		hour = c.get(Calendar.HOUR_OF_DAY);
                minute = c.get(Calendar.MINUTE);
                 
     		 
	        	datePicker.init(year, month, day, null);
	        	datePicker.invalidate();
	        	timePicker.setCurrentHour(hour);
	       		timePicker.setCurrentMinute(minute);
	       		timePicker.invalidate();

	        }
	    });
	    
	    datePicker.setSaveFromParentEnabled(false);
	    datePicker.setSaveEnabled(true);

		return dialogBox;

    }
	
	
	@Override
	public void onSaveInstanceState(Bundle arg0) {
		day = datePicker.getDayOfMonth();
    	month = datePicker.getMonth();
    	year = datePicker.getYear();
    	hour = timePicker.getCurrentHour();
    	minute = timePicker.getCurrentMinute();
		
		arg0.putInt("year", year);
		arg0.putInt("month", month);
		arg0.putInt("day", day);
		arg0.putInt("hour", hour);
		arg0.putInt("minute", minute);
		arg0.putInt("hour", hour);
		arg0.putBoolean("selected", selected);
		super.onSaveInstanceState(arg0);
	}

}
